package com.beetobe.main.schedule;

import java.util.ArrayList;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.TextView;

import com.beetobe.main.R;

public class ExpandableListAdapter extends BaseExpandableListAdapter {
	public boolean areAllItemsEnabled() {
		return true;
	}

	private Context context;
	private ArrayList<String> groups;
	private ArrayList<ArrayList<Group>> children;

	public ExpandableListAdapter(Context context, ArrayList<String> groups,
			ArrayList<ArrayList<Group>> children) {
		this.context = context;
		this.groups = groups;
		this.children = children;
	}
	
	public void addItem(Group groupItem) {
		if (!groups.contains(groupItem.getGroup())) {
			groups.add(groupItem.getGroup());
		}
		int index = groups.indexOf(groupItem.getGroup());
		if (children.size() < index + 1) {
			children.add(new ArrayList<Group>());
		}
		children.get(index).add(groupItem);
	}

	public Object getChild(int groupPosition, int childPosition) {
		return children.get(groupPosition).get(childPosition);
	}

	public long getChildId(int groupPosition, int childPosition) {
		return childPosition;
	}

	public View getChildView(int groupPosition, int childPosition,
			boolean isLastChild, View convertView, ViewGroup parent) {
		Group groupItem = (Group) getChild(
				groupPosition, childPosition);
		if (convertView == null) {
			LayoutInflater infalInflater = (LayoutInflater) context
					.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			convertView = infalInflater.inflate(R.layout.child_layout, null);
		}
		TextView tv = (TextView) convertView.findViewById(R.id.tvChild);
		tv.setText("   " + groupItem.getName());

		tv.setCompoundDrawablesWithIntrinsicBounds(0, 0, 0, 0);
		return convertView;
	}

	public int getChildrenCount(int groupPosition) {
		return children.get(groupPosition).size();
	}
	
	public Object getGroup(int groupPosition) {
		return groups.get(groupPosition);
	}

	public int getGroupCount() {
		return groups.size();
	}

	public long getGroupId(int groupPosition) {
		return groupPosition;
	}

	public View getGroupView(int groupPosition, boolean isExpanded,
			View convertView, ViewGroup parent) {
		String group = (String) getGroup(groupPosition);
		if (convertView == null) {
			LayoutInflater infalInflater = (LayoutInflater) context
					.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			convertView = infalInflater.inflate(R.layout.group_layout, null);
		}
		TextView tv = (TextView) convertView.findViewById(R.id.tvGroup);
		tv.setText(group);
		return convertView;
	}

	public boolean hasStableIds() {
		return true;
	}

	public boolean isChildSelectable(int arg0, int arg1) {
		return true;
	}
}